﻿Imports GameEngine
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework

Public Class Windmill
    Inherits MovableObject

    ''' <summary>
    ''' Shortcut reference to the bone that we are going to animate.
    ''' </summary>
    ''' <remarks></remarks>
    Private mRotorBone As ModelBone

    ''' <summary>
    ''' Store the original transform matrix for each animating bone.
    ''' </summary>
    Private mRotorTransform As Matrix

    ''' <remarks>aka mDeltaRotationValue</remarks>
    Private mRotationSpeed As Single = 5.0F

    ''' <summary>
    ''' Current animation position.
    ''' </summary>
    Private mRotorRotationValue As Single = 0.0F

    ''' <summary>
    ''' Game Screen that this object is a child of
    ''' </summary>
    Private mParent As GameScreen

    Public Sub New(ByVal GameManager As GameManager, ByVal Parent As GameScreen)
        MyBase.New(GameManager)
        mParent = Parent
        StrafeUp(100.0F)
        MoveForwards(25.0F)
    End Sub

    Public Overrides Sub LoadContent()
        MyBase.LoadContent()

        Models.Add(New MyModel)

        With Models(0)
            .MinimumDistance = 1
            .Model = GameManager.Game.Content.Load(Of Model)("windmill")
            '.Texture = GameManager.Game.Content.Load(Of Texture2D)("windmilltexture")

            ' Look up shortcut references to the bones we are going to animate.
            mRotorBone = .Model.Bones("Cylinder.002")

            ' Store the original transform matrix for each animating bone.
            mRotorTransform = mRotorBone.Transform

        End With
    End Sub

    Public Overrides Sub Update(ByVal elapsedSeconds As Single, ByVal ccp As ICollisionCandidateProvider)

        mRotorRotationValue += elapsedSeconds * mRotationSpeed

        ' Calculate matrices based on the current animation position.
        Dim rotorRotation As Matrix = Matrix.CreateRotationZ(mRotorRotationValue)

        ' Apply matrices to the relevant bones.
        mRotorBone.Transform = rotorRotation * mRotorTransform

        If IsActive Then
            ' Move and rotate windmill as per input
            RotateRight(mMovement.X * elapsedSeconds * 90)
            MoveBackwards(mMovement.Y * elapsedSeconds * 50)
        End If
        MyBase.Update(elapsedSeconds, ccp)
    End Sub

    Private mMovement As Vector2

    Public Overrides Sub HandleInput(ByVal input As GameEngine.IGamePadInputManager)
        MyBase.HandleInput(input)
        mMovement = input.MainMovement
        If input.BPressed Then IsActive = True
    End Sub

    Public Overrides Sub Draw(ByVal elapsedSeconds As Single, ByVal camera As GameEngine.Camera3D)
        MyBase.Draw(elapsedSeconds, camera)
    End Sub

    Protected Overrides Sub CollideWith(ByVal gObj As GameEngine.GameObject)
        MyBase.CollideWith(gObj)

        'If gObj.Parent IsNot Nothing Then
        '    gObj.Parent.Objects.Remove(gObj)
        'End If
        gObj.Scale = New Vector3(gObj.Scale.X, gObj.Scale.Y = 0.01F, gObj.Scale.Z)
        ForcesAndEffectsToApply = ForcesAndEffectsToApply And Not ForcesAndEffects.Collision
        GameManager.ShowNotification("Badger Badger Badger!")
    End Sub
End Class